package com.shadowtorcher.soulknife.parser;
/*
 * Copyright (C)  li, Shadow Torcher project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.shadowtorcher.soulknife.SoulKnifeDB;
import com.shadowtorcher.soulknife.tableManager.Manager;
import com.shadowtorcher.soulknife.util.Logger;
import com.shadowtorcher.soulknife.util.Metadata;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class SoulKnifeConfig {
    private static final String CONFIG_PROPERTIES = "/soulknife.config.properties";
    public static String SqlDriver;
    public static String Host;
    public static String Port;
    public static String Username;
    public static String Password;
    public static String Schemas;
    public static int SchemasVersion;

    static {
        Properties properties = new Properties();
        try {
            InputStream inputStream = SoulKnifeDB.class.getResourceAsStream(CONFIG_PROPERTIES);
            properties.load(inputStream);
            SqlDriver = properties.getProperty("driver");
            Host = properties.getProperty("url");
            Port = properties.getProperty("port", "3306");
            Username = properties.getProperty("user");
            Password = properties.getProperty("password");
            Schemas = properties.getProperty("schemas");
            SchemasVersion = Integer.parseInt(properties.getProperty("schemas_version"));
            Logger.setLogLevel(properties.getProperty("log_level", "DEBUG"));
            Manager.createTable(Metadata.class);
        } catch (IOException e) {
            e.printStackTrace();
            Logger.logE("No Config File，Give up start");
        } catch (SQLException e) {
            e.printStackTrace();
            Logger.logE("DataBase Error");
        }
    }

    public String toString() {
        return "SqlDriver: " + SqlDriver + "\n" +
                "Host: " + Host + "\n" +
                "Prot: " + Port + "\n" +
                "Schemas: " + Schemas + "\n" +
                "Username: " + Username + "\n" +
                "Password: " + Password + "\n";
    }
}
